package com.aioz.security.service.impl;

import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.*;
import com.aioz.security.entity.dto.PositionDto;
import com.aioz.security.mapper.*;
import com.aioz.security.service.CompanyInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aioz
 * @since 2022-10-18
 */
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoMapper, CompanyInfo> implements CompanyInfoService {

    @Resource
    private CompanyInfoMapper companyInfoMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private FileMapper fileMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private WelfareMapper welfareMapper;

    @Override
    public ResponseResult getInfo(Integer id) {
        Map<String, Object> detailedCompanyInfo = getCompanyInfoById(id);
        return new ResponseResult<>(200,detailedCompanyInfo);
    }

    @Override
    public ResponseResult getCompanyInfoByName(String name) {
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        Integer id = companyMapper.selectOne(wrapper).getId();
        if(id != null){
            Map<String, Object> detailedCompanyInfo = getCompanyInfoById(id);
            return new ResponseResult<>(200,detailedCompanyInfo);
        }else {
            return new ResponseResult<>(200,"暂无相关企业");
        }
    }

    public Map<String,Object> getCompanyInfoById(Integer id){
        Map<String,Object> detailedCompanyInfo = new HashMap<>();
        //查询企业岗位信息
        List<PositionDto> positions = positionMapper.getPositionByCompanyId(id);
        int positionCounts = positions.size();
        //查询企业工商信息
        QueryWrapper<CompanyInfo> companyWrapper = new QueryWrapper<>();
        companyWrapper.eq("company_id",id);
        CompanyInfo businessInfo = companyInfoMapper.selectOne(companyWrapper);
        // 查询企业基本信息
        Company basicInfo = companyMapper.selectById(id);
        //查询企业HR信息
        List<User> hrInfos = userMapper.getUsersByCompanyId(id);
        int hrCounts = hrInfos.size();
        // 查询企业相册信息
        QueryWrapper<File> fileWrapper = new QueryWrapper<>();
        fileWrapper.eq("company_id",id);
        List<File> files = fileMapper.selectList(fileWrapper);
        List<String> images = files.stream()
                .map(File::getPath)
                .collect(Collectors.toList());
        //查询企业福利信息
        List<Welfare> welfareList = welfareMapper.queryWelfareByCompanyId(id);
        List<String> welfares = welfareList.stream()
                .map(Welfare::getName)
                .collect(Collectors.toList());
        if(businessInfo == null){
            detailedCompanyInfo.put("msg","企业信息尚不完善");
            return detailedCompanyInfo;
        }
        //企业的工商信息封装到 map
        detailedCompanyInfo.put("businessInfo",businessInfo);
        //企业岗位信息封装到 map
        detailedCompanyInfo.put("positions",positions);
        //企业基本信息封装到 map
        detailedCompanyInfo.put("basicInfo",basicInfo);
        //企业HR信息封装到 map
        detailedCompanyInfo.put("hrInfos",hrInfos);
        //企业相册信息封装到 map
        detailedCompanyInfo.put("images",images);
        //企业福利信息封装到 map
        detailedCompanyInfo.put("welfares",welfares);
        detailedCompanyInfo.put("hrCounts",hrCounts);
        detailedCompanyInfo.put("positionCounts",positionCounts);
        return detailedCompanyInfo;
    }

}
